<?php
namespace app\kaoping\controller;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
class Login extends First{
    public function login(){
        if (session('?aid')) {
            $this->redirect('index/index');
        }else{
        	if (request()->isPost()) {
				if (input('name')=='') {
					$mes['state']=400;
					$mes['msg']='用户名不能为空';
				}else{
					if(input('password')==''){
						$mes['state']=400;
						$mes['msg']='密码不能为空';
					}else{
						if (input('code')=='') {
							$mes['state']=400;
							$mes['msg']='验证码不能为空';
						}else{
							$code=input('code');
							if (captcha_check($code)){
								$data['name']=input('name');
								$data['password']=md5(md5(input('password')));

								$res=Db::table('admin')
									->where($data)
									->where('type','<>',2)
								    ->find();//return $res;die;
								if ($res) {
									if ($res['hidden']==1) {
										$mes['state']=400;
										$mes['msg']='该账号已被禁用';
									}else{
										$info['ip'] = request()->ip();
										$info['time']=time();
										$info['aid']=$res['id'];
										@Db::table('login_log')->insert($info);
										@Db::table('admin')->where('id',$res['id'])->update(['last_login'=>time()]);
										session('aid',$res['id']);
										session('aname',$res['name']);//dump(session('aid'));die;
										session('atype',$res['type']);
										if($res['type']==1){
											$sc=Db::table('school')->where('aid',$res['id'])->find();session('scname',$sc['name']);
										}
										$mes['state']=200;
										$mes['msg']='登录成功';
									}
								}else{
									$mes['state']=400;
									$mes['msg']='账号密码错误';
								}
							}else{
								$mes['state']=400;
								$mes['msg']='验证码错误';
							}
						}
					}
				}
				return json($mes);
			}else{
				//$ip=request()->header();dump($ip);die;
				return view();
			}
        }
    }

    //获取验证码
	function get_code(){
		if (request()->isPost()) {
			$mobile = input('mobile');//return session('mobile');
			if(is_mobile($mobile)){
				$num=Db::table('admin')->where('tel',$mobile)->find();
				if (input('type')==1) {
					if ($num) {
						$mes['state']=400;
						$mes['msg']='该账号已被注册';
						return json($mes);
					}
				}elseif(input('type')==2){
					if (!$num) {
						$mes['state']=400;
						$mes['msg']='未找到相关账号信息';
						return json($mes);
					}
				}
				//return 111;
				$sms=model('Smscode');
				$templateid = "388333";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
				$param = rand(100000,999999); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
				$uid = "";
				//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
				$res = $sms->code($templateid,$param,$mobile,$uid);$res=json_decode($res,true);
				if ($res['code']=='000000') {
					session('mobile',$mobile);session('smsCode',$param);
					$msg['state']=200;
					$msg['msg']='验证码发送成功，请注意查收';
				}else{
					$msg['state']=400;
					$msg['msg']='验证码发送失败';
				}
			}else{
				$msg['state']=400;
				$msg['msg']='手机号格式错误！';
			}
			return json($msg);
		}
	}

	//注册
	function register(){
		if(request()->isPost()){
			$num=Db::table('user')->where('tel='.input('mobile'))->find();//return session('smsCode');
			if ($num) {
				$mes['state']=400;
				$mes['msg']='该账号已被注册';
			}else{
				$code=session('smsCode');
				if($code==input('code')){
					$data['name']=input('mobile');
					$data['tel']=session('mobile');
					$data['password']=md5(md5(input('paw')));
					$data['time']=time();
					$res=Db::table('user')->insert($data);
					if ($res) {
						session('mobile',null);
						session('smsCode',null);
						$mes['state']=200;
						$mes['msg']='注册成功';
					}
				}else{
					$mes['state']=400;
					$mes['msg']='验证码错误，请确认后重试';
				}
			}
			return json($mes);
		}else{
			return $this->fetch();
		}
	}

	//忘记密码
	function forget_paw(){
		if(request()->isPost()){
			$code=session('smsCode');
			if($code==input('code')){
				$num=Db::table('admin')->where('tel='.session('mobile'))->find();
				if ($num) {
					$mobile=session('mobile');
					$data['password']=md5(md5(input('paw')));
					$data['paw']=input('paw');
					$res=Db::table('admin')->where('tel='.session('mobile'))->update($data);
					if ($res) {
						session('mobile',null);
						session('smsCode',null);
						$mes['state']=200;
						$mes['msg']='密码修改成功';
						$sms=model('Smscode');
						$templateid = "388332";    //可在后台短信产品→选择接入的应用→短信模板-模板ID，查看该模板ID
						$param = $num['name'].','.input('paw'); //多个参数使用英文逗号隔开（如：param=“a,b,c”），如为参数则留空
						$uid = "";
						//70字内（含70字）计一条，超过70字，按67字/条计费，超过长度短信平台将会自动分割为多条发送。分割后的多条短信将按照具体占用条数计费。
						@$res = $sms->code($templateid,$param,$mobile,$uid);//$res=json_decode($res,true);
					}else{
						$mes['state']=400;
						$mes['msg']='密码修改失败';
					}
				}else{
					$mes['state']=400;
					$mes['msg']='账号不存在';
				}
			}else{
				$mes['state']=400;
				$mes['msg']='验证码错误，请确认后重试';
			}
			return json($mes);
		}
	}

    //退出登录
	public function loginout(){
		//Db::table('login_log')->where('aid='.session('aid=').' and aip='.session('aip'))->setField('last_time',session('atime'));
		session('aid',null);
		session('aname',null);
		session('atype',null);
		return redirect('login/login');
	}
}
